<?php
class BlogController extends App_Zend_Controller_Action_Shop
{
	public function indexAction()
	{
		$page=$this->_request->getParam('page');
		$item=new CItem();	
		$this->getHelper('Fenlei')->getCategories();
		$this->view->styleName = 'bulletin';
		//$blogComment = new CBlogComments();
		$itemCounts=$item->getAllItemCounts('blog');
    	Zend_Loader::loadClass("CFilter");
    	$pageArray = CFilter::filterPage ( $page, $itemCounts, 10 );
		$itemArray=$item->getAllItems($pageArray['from'],10,'blog');
		Zend_Loader::loadClass("CPage");
    	$pageString = CPage::divPagesBlog( $pageArray ['pageNumber'], $pageArray ['page']); //生成分页字符串
    	$this->view->assign ( "pageString", $pageString );
    	$this->view->assign ( "pageArray", $pageArray );
    	$this->view->assign ( "page", $page );
		$this->view->assign('itemArray',$itemArray);
		//$this->view->blogComment = $blogComment;
		if($page && $page>1){
			$this->view->webSiteTitle = 'newjerseysoccer\'s Blog, Soccer Jersey Blog, Soccer News Blog, Page '.$page;
			$this->view->webSiteDescription = 'You can find soccer jersey, discount soccer jersey on newjerseysoccer\'s blog, You are at Page '.$page;
		}else{
			$this->view->webSiteTitle = 'newjerseysoccer\'s Blog, Soccer Jersey Blog, Soccer News Blog';
			$this->view->webSiteDescription = 'You can find soccer jersey, discount soccer jersey on newjerseysoccer\'s blog';
		}
		
		$this->view->blogType = 'blogIndex';
		
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:title', 'blog');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:type', 'jersery');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:url', MY_URL);
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:image', MY_URL.'/images/logo.jpg');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:site_name', 'newjerseysoccer');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:description', $this->subDesc($itemArray[0]['content']));
	}
	
	public function detailAction()
	{
		$this->getHelper('Fenlei')->getCategories();
		$itemid=$this->_request->getParam('id');
		$this->view->styleName = 'bulletin';
		$page = intval($this->getRequest()->getParam('page'));
    	$item=new CItem();
    	$member = new CMember();
    	
    	$this->view->blogType = 'blogDetail';
    	
    	$itemArray=$item->getDetail($itemid,'blog',false);
    	$blogId = $itemArray[0]['blog_id'];
    	
    	$this->view->assign('itemArray',$itemArray);
    	$this->view->member = $member;
    	$this->view->webSiteTitle = $itemArray[0]['subject']; 
    	$this->view->webSiteDescription =$this->subDesc($itemArray[0]['content']);    	
    	
    	$this->getHelper ('layout')->getView()->headMeta()->setName('og:title', $itemArray[0]['subject']);
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:type', 'jersery');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:url', MY_URL);
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:image', MY_URL.'/images/logo.jpg');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:site_name', 'newjerseysoccer');
		$this->getHelper ('layout')->getView()->headMeta()->setName('og:description', $this->subDesc($itemArray[0]['content']));
	}
	
	public function commentAction()
	{
		if ($this->getRequest()->isPost()) {
			$ip = $this->getHelper('User')->getIp();
			$time = time();
			$blogId = $this->getRequest()->getParam('blog_id');
			$blogId = intval($blogId);
			$comment = $this->view->escape($this->getRequest()->getParam('comment'));
			if (empty($comment) || $blogId == 0) {
				$this->view->showMsg('Some Error!!!');
				return;
			}
			$blogComments = new CBlogComments();
			$row = array('blog_id'=>$blogId,
						 'comment'=>$comment,
						 'add_time'=>$time,
						 'ip'=>$ip);
			if ($blogComments->add($row)) {
				$this->view->showMsg('Comment Success, Thank you!');
			} else {
				$this->view->showMsg('Some Error!!!');
			}
		}
	}
	
	public function autoaddAction()
	{
		if ($this->getRequest()->isPost()) {
			$this->getHelper('layout')->disableLayout();
			$this->getHelper('viewRenderer')->setNoRender();
			$thisArray=$this->_request->getParams();
			$filters = array(
	    		'*'   => 'StringTrim'
			);
			$validators = array();
			
			$input = new Zend_Filter_Input($filters, $validators, $thisArray);
			
			$blog = new CBlog();
			$url = $input->url;
			$row = $blog->fetchRow(array('url=?'=>$url));
			if ($row) {
				exit();
			}
			if (empty($input->subject)) {
				exit();
			}
			/*插入到条目表 start*/
			$item=new CItem();
			$itemData=array(
							'uid'=>1,
							'flid'=>0,
							'subject'=>$input->subject,
							'item_type'=>'blog',
							'add_time'=>time(),
							'update_time'=>time(),
							'is_banned'=>'0',
					  );
			
			$itemid=$item->createItem($itemData);
	    	/*插入到条目表 end*/
			$content=htmlspecialchars_decode($input->FCKeditor1);
			$content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
			
	    	/*插入到公告表 start*/			
			$data=array(
							'itemid'=>$itemid,							
							'content'=>$content,
							'url'=>$url
						 );	
			
			$blog->add($data);
			/*插入到公告表 end*/			
			$this->view->showMsg("发表成功!",'administrator-blog-index');
		}
	}
	
	private function subDesc($desc){
		if(mb_strlen($desc)<=200){
			return strip_tags(trim($desc));
		}
		$desc = mb_substr(strip_tags(trim($desc)),0,200);
		$position = mb_strrpos($desc,' ');
		return mb_substr($desc,0,$position).'...';
	}
}